<!--
@fucntion: 教师信息
-->
<template>
<div class="module-basic-teacher">
  <el-row style="height:100%;">
    <el-col style="height:100%;">
      <table-panel :name="'教师信息'" :columns="teacherColumns" :tableData="teacherData" :showRowNumber="false" :multiSelection="true"
      :inputLabelSearchMode="true"
      :canCreate="authorities.create" :canEdit="authorities.edit" :canDelete="authorities.delete"
      :currentPage="teacherCurrentPage" :totalNum="teacherTotalNum" @page-change="onPageChange('teacher',$event)" :pageSize="pageSize"
      @table-searching="onSearching('teacher',$event)"
      @table-create="onCreate('teacher',$event)" @table-edit="onEdit('teacher', $event)"
      @table-delete="onDelete('teacher',$event)" @table-export="onExport('teacher',$event)"
      @selection-change="onSelectionChange('teacher',$event)"></table-panel>
    </el-col>
  </el-row>
  <teacher-dlg :teacherRecord="teacherRecord" :teacherRules="teacherRules" 
  :teacherDlgVisible="teacherDlgVisible" :hasResetBtn="true"
  @upload-success="handleAvatarSuccess" @close="teacherDlgVisible=false" 
  @confirm="onConfirm('teacher')" @reset-password="onResetPassword('teacher')"></teacher-dlg>
</div>
  
</template>
<script>
import TablePanel from "@/components/basic/TablePanel";
import {
  formModelInitFromTable,
  formRulesInitFromTable
} from "@/utils/formutils.js";
import { trueFalseRenderer } from "@/utils/tableutils.js";

import Dlg from "./teacher/Dlg";

//meta info
var teacher = "teacher"; // 表格名称
var columnsAttr = "Columns"; // 列
var dataAttr = "Data"; // 数据
var selectionAttr = "Selection"; // 选中
var dlgVisibleAttr = "DlgVisible"; // 对话框
var formAttr = "Form";
var recordAttr = "Record"; // 表单模型
var rulesAttr = "Rules"; // 表单验证
var curPageAttr = "CurrentPage";// 分页-当前页
var totalNumAttr = "TotalNum";// 分页-总数
var qParamAttr = "QueryParams";// 查询参数

// 教师信息-列
var teacherColumns = [
  {
    prop: "jsbh",
    label: "教师编号",
    outOfSearch:true,
    hidden: true
  },
  {
    prop: "xbh",
    label: "系编号",
    outOfSearch:true,
    hidden: true,
    rules: [{
      required: true,
      message: "必填", 
      trigger: "blur"
    }]
  },
  {
    prop:"xymc",
    label:"学院"
  },
  {
    prop:"xmc",
    label:"系"
  }, 
  {
    prop: "gh",
    label: "工号",
    rules: [{
      required: true,
      message: "必填", 
      trigger: "blur"
    },{ 
      max: 20, 
      message: "不超过20个字符", 
      trigger: 'blur' 
    }]
  },
  {
    prop: "xm",
    label: "姓名",
    rules: [{
      required: true,
      message: "必填", 
      trigger: "blur"
    },{ 
      max: 10, 
      message: "不超过10个字符", 
      trigger: 'blur' 
    }]
  },
  {
    prop: "sfds",
    label: "是否导师", 
    outOfSearch:true,
    hidden:true,
    rules: [{
      required: true,
      message:"必填",
      trigger: "blur"
    }]
  },
  {
    prop: "xb",
    label: "性别",
    width: 50,
    rules: [{
      required: true,
      message: "请选择性别",
      trigger: "blur"
    }]
  },
  {
    prop:"csrq",
    label:"出生日期",
    hidden:true,
    type:"date",
    rules:[{
      required: true,
      message: "请选择出生日期",
      trigger: "blur"
    }]
  },
  {
    prop: "zw",
    label: "职务",
    rules: [{
      required: true,
      message: "请选择职务",
      trigger: "blur"
    }]
  },
  {
    prop: "zc",
    label: "职称",
    rules: [{
      required: true,
      message: "请选择职称",
      trigger: "blur"
    }]
  },
  {
    prop: "yjfx",
    label: "研究方向",
    outOfSearch:true,
    hidden:true,
    rules: [{ 
      max: 10, 
      message: "不超过10个字符", 
      trigger: 'blur' 
    }]
  },
  {
    prop: "sj",
    label: "手机",
    hidden:true,
    rules: [{
      required: true,
      message: "必填",
      trigger: "blur"
    },{ 
      max: 20, 
      message: "不超过20个字符", 
      trigger: 'blur' 
    }]
  },
  {
    prop: "bgdh",
    label: "办公电话",
    hidden:true,
    rules: [{ 
      max: 20, 
      message: "不超过20个字符", 
      trigger: 'blur' 
    }]
  },
  {
    prop: "yx",
    label: "邮箱",
    hidden:true,
    rules: [{ 
      max: 20, 
      message: "不超过20个字符", 
      trigger: 'blur' 
    }]
  },
  {
    prop: "jsjs",
    label: "简历",
    outOfSearch:true,
    hidden:true
  },
  {
    prop: "zplj",
    label: "照片",
    outOfSearch:true,
    hidden:true
  },
  {
    prop: "rzsj",
    label: "入职时间",
    type:"date",
    hidden:true,
    rules: [{
      required: true,
      message:"必填",
      trigger: "blur"
    }]
  },
  {
    prop: "zzmm",
    label: "政治面貌",
    rules: [{
      required: true,
      message:"必填",
      trigger: "blur"
    }]
  },
  {
    prop: "jg",
    label: "籍贯",
    outOfSearch:true,
    hidden:true,
    rules: [{
      required: true,
      message:"必填",
      trigger: "blur"
    },{ 
      max: 100, 
      message: "不超过100个字符", 
      trigger: 'blur' 
    }]
  },
  {
    prop: "xw",
    label: "学位",
    hidden:true,
    rules: [{
        required: true,
        message: "请选择职称",
        trigger: "blur"
    }]
  },
  {
    prop: "xl",
    label: "学历",
    rules: [{
        required: true,
        message: "请选择职称",
        trigger: "blur"
    }]
  },
  {
    prop: "sxzy",
    label: "所学专业",
    outOfSearch:true,
    hidden:true,
    rules: [{ 
      max: 20, 
      message: "不超过20个字符", 
      trigger: 'blur' 
    }]
  },
  {
    prop: "byyx",
    label: "毕业院校",
    outOfSearch:true,
    hidden:true,
    rules: [{ 
      max: 50, 
      message: "不超过50个字符", 
      trigger: 'blur' 
    }]
  },
  {
    prop: "sfqy",
    label: "是否启用",
    type: "boolean",
    width: 80,
    outOfSearch: true,
    hidden:true,
    align: "center",
    headerAlign: "center",
    renderer: trueFalseRenderer
  },
  {
    prop:"yhm",
    label:"用户名"
  }
];

export default {
  name: "Teacher",
  components: {
    "table-panel": TablePanel,
    "teacher-dlg":Dlg
  },
  methods: {
    onSearching: function(tname, params) {//查询
      var me = this;
      let url = "/jcJsxxb";// /static/data/teacher.json

      if(url !== "") {
        me[tname+qParamAttr] = {};
        me[tname+qParamAttr] = params;
        if (params.isAccurate) {
          me[tname+qParamAttr] = params.paramPairs;
        } else {
          me[tname+qParamAttr].keywords = params.query;
        }
        me.getRequest(url, me[tname+qParamAttr]).then(resp => {
            if(resp.status == 200) {
              me[tname+dataAttr] = resp.data.results;
              me[tname+totalNumAttr] = resp.data.total;
            }
        });
      }
    },
    onCreate: function(tname) {// 新增
      var me = this;
      me[tname + recordAttr] = formModelInitFromTable(me[tname + columnsAttr]);
      me[tname + recordAttr].sfqy = "1"; // 创建时，是否启用选中
      me[tname + recordAttr].sfds = "1"; // 创建时，是否导师选中
      
      me[tname + dlgVisibleAttr] = true;
    },
    onEdit: function(tname, row) {// 编辑
      var me = this;
      me[tname + recordAttr] = row;
      me[tname + dlgVisibleAttr] = true;
    },
    onDelete: function(tname, row) {// 删除
      var me = this;
      me.$confirm("确定删除这条记录吗?", "提示", {
          type: "warning"
      }).then(() => {
          me.deleteRequest(`/jcJsxxb/${row.jsbh}`).then((resp)=>{
            if(resp.status == 200) {
              me.$message.success("删除成功!");
              me.onSearching(department,{});// 重新加载表格数据
            }else{
              me.$message.error('删除失败！');
            }
          });
      }).catch(() => {});
    },
    onExport: function(tname, params) {// 导出
      var me = this;
      me.$message.info("发送导出请求，返回下载路径");
    },
    onSelectionChange: function(tname, data) {// 选中改变
      var me = this;
      // 缓存记录，按照角色编号，查询对应的选中权限节点数组
      me[tname + selectionAttr] = data;
    },
    onPageChange:function(tname,page){// 分页改变
      var me = this;
      let url = "/jcJsxxb";

      if(url !== ""){
        me[tname+curPageAttr] = page;
        me[tname+qParamAttr]["page"] = page;
        me.getRequest(url, me[tname+qParamAttr]).then(resp => {
            if(resp.status == 200) {
              me[tname+dataAttr] = resp.data.results;
              me[tname+totalNumAttr] = resp.data.total;
            }
        });
      }

    },
    onConfirm: function(tname) {// 对话框-确认
      let me = this;
      if(me[teacher + recordAttr].jsbh == ""){
        // 把工号放进用户名
        me[teacher + recordAttr].yhm = me[teacher + recordAttr].gh;
      }
      // 发送请求 /static/data/testSuc.json
      me.postRequest("/jcJsxxb",me[teacher + recordAttr]).then(resp => {
        if (resp.status == 200) {
          me.$message.success("提交成功！");
          //最后，隐藏对话框
          me[tname + dlgVisibleAttr] = false;
          me.onSearching(teacher, {});// 重新查一遍
        } else {
          me.$message.error("服务器发生错误，代码：" + resp.status);
        }
      });
    },
    handleAvatarSuccess:function(res, file) {//上传成功之后
      var me = this;
			me[teacher+recordAttr]['zplj'] = res.path;//生成一个服务器路径，保存起来
      me.imageUrl = URL.createObjectURL(file.raw);// 显示照片
    },
    onResetPassword:function(tname){// 重置密码
      var me = this;
      var teacher = me[tname + recordAttr];
      console.log("user=",teacher)
      me.$confirm("确定要重置用户"+teacher.xm+", "+teacher.yhm+"的密码吗？", "提示", {
        type: "warning"
      }).then(() => {
        // TODO 重置密码
        me.putRequest("/jcYhxxb/resetPwd",{
          yhm:teacher.yhm
        }).then((resp)=>{
          if(resp.status == 200) {
            me[tname + dlgVisibleAttr] = false;
            me.$message.success(resp.data.msg);
          } else {
            me.$message("服务器发生错误，代码：" + resp.data.msg);
          }
        });
      }).catch(() => {});
    }
  },
  data() {
    return {
      authorities:{
        create:true,
        delete:true,
        edit:true,
        search:true
      },// 权限控制
      [teacher + columnsAttr]: teacherColumns, // 表格-列
      [teacher + dataAttr]: [], // 表格的数据
      [teacher + selectionAttr]: [], // 当前选中
      [teacher + dlgVisibleAttr]: false, // 对话框是否可见
      [teacher + recordAttr]: {}, // 表单模型
      [teacher + rulesAttr]: {},// 表单验证
      [teacher + curPageAttr]:1,// 当前页
      [teacher + totalNumAttr]:0,// 总数
      [teacher + qParamAttr]:{},// 查询参数
      pageSize:20,// 每页数量
      
    };
  },
  created: function() {
    var me = this;
    // 添加权限控制
    // me.authorities = me.$route.meta.authorities;

    // 表单模型初始化
    me[teacher + recordAttr] = formModelInitFromTable(me[teacher + columnsAttr]);
    // 表单验证初始化
    me[teacher + rulesAttr] = formRulesInitFromTable(me[teacher + columnsAttr]);

    
    me.onSearching(teacher, {});// 重新查一遍

  }
};
</script>
<style lang="scss">
.module-basic-teacher {
  height: 100%;
}
</style>


